package main

import (
	"encoding/json"

	myrpc "github.com/2217263633/kakarpc"
)

type MysqlService struct{}

func (m *MysqlService) QueryData(sql string, res *map[string]interface{}) error {
	resu, err := CusMySql.QuerySql(sql)
	// logger.Info(resu, err)
	(*res)["data"], _ = json.Marshal(resu)

	myrpc.ErrDeal(err, res)
	return nil
}

func (m *MysqlService) InsertData(sql string, res *map[string]interface{}) error {
	_, err := CusMySql.QueryIdlimit1(sql)
	myrpc.ErrDeal(err, res)
	return nil
}

func (m *MysqlService) PageSql(sql myrpc.SqlStruct, res *map[string]interface{}) error {
	resu, total, size, err := CusMySql.PageSql(sql)
	(*res)["data"], _ = json.Marshal(resu)
	(*res)["total"] = total
	(*res)["size"] = size
	myrpc.ErrDeal(err, res)
	return nil
}

func (m *MysqlService) DropTable(sql string, res *map[string]interface{}) error {
	CusMySql.DropTable(sql)
	myrpc.ErrDeal(nil, res)
	return nil
}

func (m *MysqlService) InsertTable(sql string, res *map[string]interface{}) error {
	err := CusMySql.InsertTable(sql)
	myrpc.ErrDeal(err, res)
	return nil
}

func (m *MysqlService) InsertTableId(sql string, res *map[string]interface{}) error {
	id, err := CusMySql.InsertTableId(sql)
	(*res)["id"] = id
	myrpc.ErrDeal(err, res)
	return nil
}

// table_name string, columns_sql string, values_sql string, where_sql string
func (m *MysqlService) InsertTablesSql(sql MysqlAType, res *map[string]interface{}) error {
	err := CusMySql.InsertTablesSql(sql.Table_name, sql.Columns_sql, sql.Values_sql, sql.Where_sql)
	myrpc.ErrDeal(err, res)
	return nil
}
